package com.gmzg.gmzgoffice.works;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.test.mock.MockApplication;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.gmzg.gmzgoffice.GMZGApplication;
import com.gmzg.gmzgoffice.R;
import com.gmzg.gmzgoffice.entity.Approval;

import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class VacateActivity extends AppCompatActivity implements SimpleCalendarDialogFragment.setOnTimeListener {

    @BindView(R.id.toolbar_title)
    TextView mToolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.start_time_tv)
    TextView mStartTimeTv;
    @BindView(R.id.end_time_tv)
    TextView mEndTimeTv;
    @BindView(R.id.type_tv)
    TextView mTypeTv;
    @BindView(R.id.days_et)
    EditText mDaysEt;
    @BindView(R.id.reason_et)
    EditText mReasonEt;
    @BindView(R.id.approver_tv)
    TextView mApproverTv;
    @BindView(R.id.remark_et)
    EditText mRemarkEt;
    private SimpleCalendarDialogFragment mDialogFragment;
    private SimpleDateFormat mSimpleDateFormat;
    private int flag;
    private AlertDialog mDialog;
    private Date startDate;
    private Date endDate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vacate);
        ButterKnife.bind(this);
        mToolbarTitle.setText("我的请假");
        mSimpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        setSupportActionBar(mToolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowTitleEnabled(false);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case android.R.id.home:
                finish();
                break;
        }
        return true;
    }

    @OnClick({R.id.start_time_tv, R.id.end_time_tv, R.id.type_tv, R.id.submit_btn, R.id.approver_tv})
    public void onClick(View view) {
        String userName = ((GMZGApplication) getApplication()).getUserName();
        switch (view.getId()) {
            case R.id.start_time_tv:
                flag = R.id.start_time_tv;
                showDialogFragment("开始时间");
                break;
            case R.id.end_time_tv:
                flag = R.id.end_time_tv;
                showDialogFragment("结束时间");
                break;
            case R.id.type_tv:
                final String[] types = {"事假", "病假", "年假", "调休", "婚嫁", "产假", "陪产假", "路途假", "其他"};
                showDialog(types, mTypeTv);
                break;
            case R.id.submit_btn:
                String type = mTypeTv.getText().toString().trim();
                String startTime = mStartTimeTv.getText().toString().trim();
                String endTime = mEndTimeTv.getText().toString().trim();
                String approver = mApproverTv.getText().toString().trim();
                String s = "请选择(必填)";
                if (type.equals(s) || startTime.equals(s) || endTime.equals(s) || approver.equals(s)) {
                    Toast.makeText(this, "请填写必填选项", Toast.LENGTH_SHORT).show();
                } else {
                    final Approval approval = new Approval();
                    approval.setUser_name(userName);
                    approval.setStart_time(startDate);
                    approval.setLeave_time(endDate);
                    approval.setApproval_type("请假");
                    approval.setApproval_Status(false);
                    approval.setApproval_number(System.currentTimeMillis());
                    approval.setLeave_type(type);
                    String day = mDaysEt.getText().toString();
                    if (day != null) {
                        approval.setServal_day(Integer.parseInt(day));
                    }
                    approval.setReason(mReasonEt.getText().toString());
                    approval.setWho_approval(approver);
                    approval.setRemark(mRemarkEt.getText().toString());
                    approval.save(new SaveListener<String>() {
                        @Override
                        public void done(String s, BmobException e) {
                            if (e == null){
//                                Intent intent = new Intent(VacateActivity.this, LeaveApplicationActivity.class);
//                                intent.putExtra(Constant.APPROVAL,approval);
//                                intent.putExtra(Constant.TYPE,0);
//                                startActivity(intent);
                                finish();
                                Toast.makeText(VacateActivity.this, "发送成功", Toast.LENGTH_SHORT).show();
                            }else {
                                Toast.makeText(VacateActivity.this, "发送失败", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
                break;
            case R.id.approver_tv:
                final String[] approvers01 = {"TOP","GD","1234","5678"};
                showDialog(approvers01, mApproverTv);
                break;
        }
    }

    private void showDialog(final String[] types, final TextView textView) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setSingleChoiceItems(types, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                textView.setText(types[which]);
                mDialog.dismiss();
            }
        });
        mDialog = builder.create();
        mDialog.show();
    }

    //显示日历
    private void showDialogFragment(String title) {
        if (mDialogFragment == null) {
            mDialogFragment = new SimpleCalendarDialogFragment();
        }
        mDialogFragment.setTitle(title);
        mDialogFragment.setListener(this);
        if (!mDialogFragment.isAdded()) {
            mDialogFragment.show(getSupportFragmentManager(), title);
        }
    }

    @Override
    public void setTime(Date date) {
        String time = mSimpleDateFormat.format(date);
        if (mDialogFragment != null) {
            if (flag == R.id.start_time_tv) {
                startDate = date;
                mStartTimeTv.setText(time);
            } else {
                endDate = date;
                mEndTimeTv.setText(time);
            }
        }
    }

}
